<template>
  <div>
    <t-popup
      v-model="visible" 
      placement="center"
      style="width: 90vw"
      @close="showPopuphidee()"
    >
      <div class="popup-content">
        <div class="proptitle">{{ titles }}{{ endtext || "新增" }}</div>
        <div class="form-content">
          <t-form
            :inline="inline"
            :data="formData"
            :rules="rules"
            ref="rulesForm"
            label-position="right"
          >
            <t-form-item
              label="账户名称："
              label-width="170"
              prop="username"
              name="username"
            >
              <t-input
                v-model.trim="formData.username"
                maxlength="60"
                clearable
                borderless
                align="right"
                placeholder="请输入账户名称"
                style="width: 200px"
              ></t-input>
            </t-form-item>
            <t-form-item
              label="手机号："
              label-width="170"
              prop="mobile"
              name="mobile"
            >
              <t-input
                v-model.trim="formData.mobile"
                maxlength="60"
                clearable
                borderless
                align="right"
                placeholder="请输入手机号"
                style="width: 200px"
              ></t-input>
            </t-form-item>

            <t-form-item
              label="密码："
              label-width="170" 
              prop="password"
              name="password"
              v-if="!formData.id"
            >
              <t-input
                v-model.trim="formData.password"
                maxlength="60"
                clearable
                borderless
                align="right"
                placeholder="请输入密码"
                style="width: 200px"
              ></t-input>
            </t-form-item>
            <t-form-item
              label="督课管理权限："
              align="right"
              label-width="170" 
              v-if="authority([3])"
            >
              <div style="display: flex; justify-content: end; width: 100%"> 
                <t-switch v-model="formData.is_qstatus" :customValue="['2','1']"></t-switch>
              </div>
            </t-form-item>
            <t-form-item
              label="是否修改密码："
              align="right"
              label-width="170"
              v-if="formData.id"
            >
              <div style="display: flex; justify-content: end; width: 100%">
                <t-switch v-model="passwordSwitch"></t-switch>
              </div>
            </t-form-item>
            <t-form-item
              label="修改密码："
              label-width="170"
              prop="passwordEdit"
              name="passwordEdit"
              v-if="passwordSwitch"
            >
              <t-input
                v-model.trim="formData.passwordEdit"
                maxlength="60"
                clearable
                borderless
                align="right"
                placeholder="请输入新密码"
                style="width: 200px"
              ></t-input>
            </t-form-item>
          </t-form>
        </div>
        <div
          class="t-form__footer"
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 10px;
          "
        >
          <t-button
            style="margin: 0 10px; width: 100%"
            theme="primary"
            :disabled="loading"
            @click="submitForm()"
          >
            {{ endtext || "新增" }}
          </t-button>
        </div>
      </div>
    </t-popup>
  </div>
</template>
  <script>
import { getStorage } from "@/utils/util";

export default {
  props: {
    titles: {
      typeof: String,
      default: "超管",
    },
  },
  data() {
    return {
      visible: false,
      passwordSwitch: false,
      endtext: "",
      formData: {},
      rules: {
        region_name: [
          { required: true, message: "请输入地区名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入账户名称", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        passwordEdit: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        group_name: [
          { required: true, message: "请选择权限", trigger: "blur" },
        ],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.userInfo = Number(JSON.parse(getStorage("groupId"))); //权限
  },
  methods: {
    showPopuphidee() {
      // 关闭弹窗
      this.formData = {};
      this.visible = false;
      this.passwordSwitch = false;
    },
    showends(row, text) {
      this.visible = true;
      console.log(row, "-----row");
      if (text) this.endtext = text;
      if (row) this.formData = { ...row };
    },
    authority(groupArr = [0]) {
      // 权限筛选
      let userInfo = this.userInfo;
      // 当前权限和传来的权限组进行对比
      if (groupArr.some((item) => Number(item) === userInfo)) return true;
      return false;
    },
    submitForm() {
      let vm = this;
      console.log("新增成功1", vm.$refs.rulesForm);
      vm.$refs["rulesForm"].validate().then((valid) => {
        if (valid === true) {
          let objs = {
            mobile: vm.formData.mobile,
            username: vm.formData.username,
          };
          if(vm.formData.is_qstatus) objs.is_qstatus = vm.formData.is_qstatus;
          if (vm.formData.id) {
            // 如果是编辑 添加id和新密码
            objs.id = vm.formData.id;
            if (vm.passwordSwitch) {
              objs.password = vm.formData.passwordEdit;
              vm.showPopuphidee();
             return vm.$dialog.confirm({
                title: "提示",
                content: "您确定要修改密码吗？",
                confirmBtn: "确定",
                cancelBtn: "取消",
                onConfirm: () => {
                  // 确认按钮点击事件
                  console.log("点击了按钮");
                  vm.$emit("endlists", objs);
                  //   vm.showPopuphidee();
                },
              });
            }
            vm.showPopuphidee();
            
            vm.$emit("endlists", objs);
          } else {
            // 如果是新建
            objs.password = vm.formData.password;
            vm.$emit("endlists", objs);
            vm.showPopuphidee();
          }

          console.log(valid, "新增成功2");
        }
      });
    },
  },
};
</script>
  
   
  <style scoped>
.popup-content {
  padding: 16px;
  background: #fff;
  border-radius: 12px;
  width: 90vw;
  max-width: 500px;
  box-sizing: border-box;
}

.popup-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.empty-tips {
  padding: 8px 0;
  color: var(--td-gray-color-6);
  text-align: center;
}

@media (max-width: 640px) {
  .popup-content {
    width: 100%;
    overflow-y: auto;
  }

  :deep(.t-form-item__label) {
    font-size: 14px;
  }

  :deep(.t-picker__toolbar) {
    padding: 8px 12px;
  }
}
</style>
      
      <style lang="scss" scoped>
.custom-input {
  :deep(.t-t-input--default) {
    font-size: 20px; /* 调整输入框 placeholder 字体大小 */
  }
}
</style>
      
      <style>
.t-form__label--right {
  font-size: 14px;
}
.t-cell__title-text {
  font-size: 14px;
}
.t-form__label--left {
  font-size: 14px; /* 调整左侧标签字体大小 */
}

.t-form__label {
  padding: 0;
}

.t-input--default {
  font-size: 13px;
}

.t-input--default::placeholder {
  color: gray; /* 设置颜色 */
  font-size: 14px !important; /* 设置字体大小 */
  border: none;
  /* 其他样式，例如字体系列 */
}

.t-form__item {
  padding-left: 10px;
  padding-right: 10px;
}

.t-input--default {
  font-size: 14px; /* 调整输入框字体大小 */
  border-bottom: none !important; /* 移除默认下划线 */
}
.t_cell {
  color: #000;
  font-size: 14px;
}
.t-cell__note {
  font-size: 14px;
}
.proptitle {
  font-size: 16px;
  font-weight: 600;
  padding: 10px 0;
}
.form-content {
  min-height: 0 !important;
  max-height: 70vh;
  overflow-y: auto;
}
</style>